<?PHP
  class DataField{
    private $id          = -1;
    private $dataType    = null;
    private $dataTypeID  = -1;
    private $displayName = "";
    private $dataName    = "";
    private $validatorID = -1;
    private $validator   = null;

    public static function getByFormAndName(DataType $type, $name){
      $res = new DataField();
      $typeID = $GLOBALS['db']->EscapeString($type->getID());
      $name = $GLOBALS['db']->EscapeString($name);
      
      $field = $GLOBALS['db']->ReadRow("SELECT * FROM {'dbprefix'}datafields WHERE dataType = '".$typeID."' AND dataName = '".$name."'");
      $res = new DataField();
      $res->id          = $field->id;
      $res->dataTypeID  = $field->dataType;
      $res->dataType    = $dataType;
      $res->displayName = $field->displayName;
      $res->dataName    = $field->dataName;
      $res->validatorID = $field->validator;
      
      return $res;
    }

    public static function getByDataType($dataType){
      $res = array();
      $dataTypeID = $GLOBALS['db']->EscapeString($dataType->getID());
      $fields = $GLOBALS['db']->ReadRows("SELECT * FROM {'dbprefix'}datafields WHERE datatype = '".$dataTypeID."'");
      foreach($fields as $field){
        $fieldObj = new DataField();
        $fieldObj->id          = $field->id;
        $fieldObj->dataTypeID  = $field->dataType;
        $fieldObj->dataType    = $dataType;
        $fieldObj->displayName = $field->displayName;
        $fieldObj->dataName    = $field->dataName;
        $fieldObj->validatorID = $field->validator;
        $res[] = $fieldObj;
      }
      return $res;
    }

    public function getDataName(){
      return $this->dataName;
    }

    public function getValidator(){
      if($this->validator == null){
        $this->validator = new DataValidator($this->validatorID);
      }
      return $this->validator;
    }

    public function validate($value){
      return $this->getValidator()->validate($value);
    }

    public function getLastError(){
      return $this->getValidator()->getLastError();
    }
  }
?>